package org.testis.data.parser;

import org.testis.data.*;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.nicmodule.data.NicModuleDataObject;
import org.testis.serialportmodule.data.SerialPortModuleDataObject;
import org.testis.testdriver.AtofEnumaration.AtofSerialPortModuleAction;
import java.util.LinkedHashMap;

/**
 *
 * @author nurhat
 */
public class SerialPortModuleDop implements TestStepDop {

    /**
     * Parsing data and creating a NicModule
     * Returns null if any required attribute not given
     * @param data
     * @return
     */
    public TestStepDataObject convert(TestStepDataObject data) {
        SerialPortModuleDataObject dataObj =
                new SerialPortModuleDataObject(data);
        LinkedHashMap<String, String> map = data.getDataToApply();

        Integer comPortNo = null;
        Integer baudRate = null;
        Integer stopBits = null;
        Integer dataBits = null;
        Integer parity = null;
        Integer flowControl = null;
        String networkIp = null;
        Integer networkPort = null;
        Boolean isDirectSerial = true;
        String configName = null;
        String command = null;
        String verifyStr = null;
        if (map.get(AtofModuleParameterNames.SPM_COM_PORT_NO) != null) {
            comPortNo =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_COM_PORT_NO).
                    toString());
            dataObj.setComPortNo(comPortNo);

        }
        if (map.get(AtofModuleParameterNames.SPM_VERIFY_STR) != null) {
            dataObj.setVerifyString(map.get(AtofModuleParameterNames.SPM_VERIFY_STR));
        }

        if (map.get(AtofModuleParameterNames.SPM_BAUD_RATE) != null) {
            baudRate =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_BAUD_RATE).
                    toString());
            dataObj.setBaudRate(baudRate);
        }
        if (map.get(AtofModuleParameterNames.SPM_COMMAND) != null) {
            command =
                    map.get(
                    AtofModuleParameterNames.SPM_COMMAND).
                    toString();
            dataObj.setCommand(command);
        }
        if (map.get(AtofModuleParameterNames.SPM_CONFIG_NAME) != null) {
            configName =
                    map.get(
                    AtofModuleParameterNames.SPM_CONFIG_NAME).
                    toString();
            dataObj.setConfigName(configName);
        }
        if (map.get(AtofModuleParameterNames.SPM_DATA_BITS) != null) {
            dataBits =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_DATA_BITS).
                    toString());
            dataObj.setDataBits(dataBits);
        }
        if (map.get(AtofModuleParameterNames.SPM_FLOW_CONTROL) != null) {
            flowControl =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_FLOW_CONTROL).
                    toString());
            dataObj.setFlowControl(flowControl);
        }
        if (map.get(AtofModuleParameterNames.SPM_NETWORK_IP) != null) {
            networkIp =
                    map.get(
                    AtofModuleParameterNames.SPM_NETWORK_IP).
                    toString();
            dataObj.setNetworkIp(networkIp);
            isDirectSerial = false;
        }
        if (map.get(AtofModuleParameterNames.SPM_NETWORK_PORT) != null) {
            networkPort =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_NETWORK_PORT).
                    toString());
            dataObj.setNetworkPort(networkPort);
        }
        if (map.get(AtofModuleParameterNames.SPM_PARITY) != null) {
            parity =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_PARITY).
                    toString());
            dataObj.setParity(parity);
        }
        if (map.get(AtofModuleParameterNames.SPM_STOP_BITS) != null) {
            stopBits =
                    Integer.parseInt(map.get(
                    AtofModuleParameterNames.SPM_STOP_BITS).
                    toString());
            dataObj.setStopBits(stopBits);
        }

        if ((comPortNo == null) && (networkIp == null) && !data.getActionType().equals(AtofSerialPortModuleAction.SendCommand.name())) {
            LogSenderService.putMessageOnQueue("SerialPortModuleDop",
                    AtofLogLevel.ERROR,
                    "SerialPortModuleDataObject is not correctly given." +
                    AtofModuleParameterNames.SPM_COM_PORT_NO +
                    " or " +
                    AtofModuleParameterNames.SPM_NETWORK_IP +
                    " must be given.",
                    data.getMessageId());
            return null;
        }

        dataObj.setIsDirectSerial(isDirectSerial);

        return dataObj;

    }
}

